package com.heima.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.heima.mapper.SkuMapper;
import com.heima.pojo.entity.SkuClass;
import com.heima.pojo.dto.SkuSearchDto;
import com.heima.pojo.dto.SkufyDto;
import com.heima.service.SkuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class SkuServiceImpl extends ServiceImpl<SkuMapper, SkuClass> implements SkuService {

    @Autowired
    private SkuMapper skuMapper;

    private final static Integer USER_ZERO = 0;
    private final static Integer USER_ONE = 1;

    private final static Integer USER_THREE = 10;

    /**
     * 商品类型搜索
     * @param skufy
     * @return
     */
    @Override
    public SkuSearchDto<SkuClass> search(SkufyDto skufy) {
       // 设置默认值
        int pageIndex = skufy.getPageIndex() == null || skufy.getPageIndex() < USER_ONE ? USER_ONE : skufy.getPageIndex();
        int pageSize = skufy.getPageSize() == null || skufy.getPageSize() < USER_ONE ? USER_THREE : skufy.getPageSize();

        // 开启分页
        PageHelper.startPage(pageIndex, pageSize);

        // 查询数据（返回 Page<List>）
        List<SkuClass> skuList = skuMapper.skuSearch(skufy);

        // 创建 PageInfo 获取分页信息
        PageInfo<SkuClass> pageInfo = new PageInfo<>(skuList);

        // 封装结果
        SkuSearchDto<SkuClass> result = new SkuSearchDto<>();
        result.setPageIndex(String.valueOf(pageInfo.getPageNum()));
        result.setPageSize(String.valueOf(pageInfo.getPageSize()));
        result.setTotalCount(String.valueOf(pageInfo.getTotal()));
        result.setTotalPage(String.valueOf(pageInfo.getPages()));
        result.setCurrentPageRecords(skuList);// 当前页记录

        return result;

    }

    /**
     * 删除商品类型
     * @param classId
     * @return
     */
    @Override
    public boolean delect(Long classId) {

        log.info("开始删除商品类型，classId: {}", classId);
        int result = skuMapper.deleteById(classId);
        log.info("删除结果，影响行数: {}", result);
        return result > USER_ZERO;

    }

    /**
     * 修改商品类型
     * @param skuClass
     */
    @Override
    public boolean putClassld(SkuClass skuClass) {
      return   skuMapper.putClassl(skuClass);
    }
}
